package com.jolink.omiis.wechat.api.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jolink.omiis.wechat.api.ITemplateMessageApi;

public class TemplateMessageApiImpl extends WeChatApiBaseService implements ITemplateMessageApi {
	private static Logger logger = LoggerFactory.getLogger(TemplateMessageApiImpl.class);

	@SuppressWarnings("unchecked")
	@Override
	public <T, V> T sendTemplateMessage(String appId, String openId, V v) {
		long t1 = System.currentTimeMillis();
		logger.info("调用腾讯模板信息接口发送信息，AppId：{}，内容：{}。", appId, v);
		String uri =MESSAGE_TEMPLATE_SEND;
//		String uri = "https://api.weixin.qq.com/cgi-bin/message/template/send?";
		Map<String, String> reqParams = new HashMap<String, String>();
		Map<String, String> companyInfo = getCompanyInfo(appId);
		reqParams.put("access_token", companyInfo.get("authorizer_access_token"));
		logger.info("发送信息，appId：{}，companyInfo：{}，内容：{}。", appId, companyInfo, v);
		String result = executePost(uri, reqParams, (String) v);
		Map<String, String> resultMap = result2Map(result);
		long t2 = System.currentTimeMillis();
		logger.info("调用模板信息接口发送结果：{}，本次通讯耗时：{}毫秒。", result, (t2 - t1));
		return (T) result;
	}
}
